package com.css.fxfzfxqh.modules.modelManagement.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.modelManagement.param.AddEconomicLossesParam;
import com.css.fxfzfxqh.modules.modelManagement.param.AddPersonnelCasualtiesParam;
import com.css.fxfzfxqh.modules.modelManagement.param.PagePersonnelCasualtiesParam;
import com.css.fxfzfxqh.modules.modelManagement.service.EconomicLossesService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("economicLosses")
@Slf4j
public class EconomicLossesController {

    @Autowired
    private EconomicLossesService economicLossesService;

    /**
     * 新增
     * @param param
     * @return
     */
    @PostMapping("save")
    public RestResponse save(@RequestBody AddEconomicLossesParam param) {
        try {
            if (economicLossesService.getSizeByName(param.getModelName()) != 0) {
                return RestResponse.fail("模型名称重复!");
            }
            economicLossesService.save(param);
            return RestResponse.succeed("添加成功!");
        } catch (Exception e) {
            log.error("save error:{}", e.getMessage(), e);
            return RestResponse.fail("添加失败!");
        }
    }

    /**
     * 分页
     * @param param
     * @param request
     * @return
     */
    @GetMapping("page")
    public RestResponse page(PagePersonnelCasualtiesParam param, HttpServletRequest request) {
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        try {
            return RestResponse.succeed(economicLossesService.page(param, curPage, pageSize));
        } catch (Exception e) {
            log.error("page error:{}", e.getMessage(), e);
            return RestResponse.fail("查看失败!");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    public RestResponse deleteById(String id) {
        try {
            economicLossesService.deleteById(id);
            return RestResponse.succeed(200,"删除成功!");
        } catch (Exception e) {
            log.error("deleteById error:{}", e.getMessage(), e);
            return RestResponse.fail(500,"删除失败!");
        }
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @PostMapping("updateById")
    public RestResponse updateById(@RequestBody AddEconomicLossesParam param) {
        try {
            if (economicLossesService.getSizeByName(param.getModelName()) > 1) {
                return RestResponse.fail("模型名称重复!");
            }
            economicLossesService.updateById(param);
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            log.error("updateById error:{}", e.getMessage(), e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("getById")
    public RestResponse getById(String id) {
        try {
            return RestResponse.succeed(economicLossesService.getById(id));
        } catch (Exception e) {
            log.error("getById error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }
}
